<template>
	<div class="form-control">
		<el-form :model="internalUser" label-width="auto" style="width: 100%">
			<el-form-item label="原密码">
				<el-input
					 v-model="internalUser.oldPassword"
					 type="password"
					 placeholder="请输入原密码"
				  />
			</el-form-item>
			<el-form-item label="新密码">
				<el-input
					 v-model="internalUser.password"
					 type="password"
					 placeholder="请输入新密码"
				  />
			</el-form-item>
			<el-form-item label="确认密码">
				<el-input
					 v-model="internalUser.password2"
					 type="password"
					 placeholder="请再次输入新密码"
				  />
			</el-form-item>
			
		</el-form>
	</div>
</template>

<script>
export default {
	props: {
		user: {
			type: Object,
			required: true
		}
	},
	data(){
		return {
			internalUser: { ...this.user } // 内部使用的用户对象
		}
	},
	watch: {
		internalUser: {
		handler(newVal) {
			if (this.updatingFromParent) {
			// 如果是父组件更新导致的，不再 emit，避免死循环
			this.updatingFromParent = false
			return
			}
			this.$emit("update:user", { ...newVal });
		},
		deep: true
		},
		user: {
		handler(newVal) {
			this.updatingFromParent = true
			this.internalUser = { ...newVal };
		},
		immediate: true,
		deep: true
		}
	},
}
</script>

<style scoped>
.form-control {
	padding: 30px 30px 10px;
}
</style>